package com.fiveflavors.suittar.gui.mainpanel.view;

import java.net.URL;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import twitter4j.DirectMessage;
import twitter4j.User;

import com.fiveflavors.suittar.SuittarCommand;
import com.fiveflavors.suittar.gui.utils.Converter;
import com.fiveflavors.suittar.resources.SuittarResourceManager;

public class SuittarDirectMessageView {


	public static StringBuffer getContents(SuittarTimeLineViewBean timeLineViewBean,List<DirectMessage> sent,List<DirectMessage> receive) {

		StringBuffer htmlContents = new StringBuffer();

		htmlContents.append("<HTML>");
		htmlContents.append("<BODY>");

		htmlContents.append("<DIV ALIGN=\"RIGHT\">");
		URL refreshURL = SuittarResourceManager.getResourceUrl(SuittarResourceManager.ICON_REFRESH);
		htmlContents.append("<A HREF=\"http://suittar20/command?command=" + SuittarCommand.COMMAND_SHOW_DIRECT_MESSAGE + "\">");
		htmlContents.append(Converter.convertURL2ImgTagHtml(refreshURL));
		htmlContents.append("</A>&nbsp;");
		htmlContents.append("</DIV>");

		//Main Contents(Header) - <TABLE>Tag
		htmlContents.append(getStatusTableDeclare());

		//merge send and receive messages.sort by createAt.
		TreeSet<DirectMessage> directMsgSet = new TreeSet<DirectMessage>(new DirectMessageComparator());
		directMsgSet.addAll(sent);
		directMsgSet.addAll(receive);
		
		//Main Contents (Row) - <TR><TD></TD></TR>
		for(DirectMessage directMsg: directMsgSet) {
			htmlContents.append(getStatusRowContents(directMsg,timeLineViewBean));
		}

		//Main Contents(Closer) - </TABLE>Tag
		htmlContents.append(getStatusTableCloser());

		//View Footer
		htmlContents.append(getFooterContents(timeLineViewBean));
		
		htmlContents.append("</BODY>");
		htmlContents.append("</HTML>");
		
		return htmlContents;
	}

	public static StringBuffer getStatusTableDeclare() {
		return new StringBuffer("<TABLE WIDTH=\"100%\">");
	}
	
	public static StringBuffer getStatusTableCloser() {
		return new StringBuffer("</TABLE>");
	}
	
	public static StringBuffer getStatusRowContents(DirectMessage directMsg,SuittarTimeLineViewBean timeLineViewBean) {

		StringBuffer buf = new StringBuffer();
		String replayBGColor = "";
		boolean senderIsOwner = false;

		if (directMsg.getSenderId() == timeLineViewBean.getMyUser().getId()) {
			senderIsOwner = true;
		}
		
		if (!senderIsOwner) {
			replayBGColor = " BGCOLOR=\"#ffdead\"";
		}

		User sender = directMsg.getSender();
		User recipient = directMsg.getRecipient();
		User leftUser = (senderIsOwner) ? recipient: sender;
		
		buf.append("<TR>");

		buf.append("<TD ROWSPAN=\"3\" WIDTH=\"53\" valign=\"top\"" +  replayBGColor + ">");
		buf.append("<A HREF=\"http://suittar20/command?command=" + SuittarCommand.COMMAND_SHOW_PROFILE + "&id=" + sender.getId() + "\">");
		buf.append(Converter.convertURL2ImgTagHtml(sender.getProfileImageURL()));
		buf.append("</A>");			
		buf.append("</TD>");

		buf.append("<TD ROWSPAN=\"3\" WIDTH=\"48\" VALIGN=\"top\"" +  replayBGColor + ">");
		buf.append("<A HREF=\"http://suittar20/command?command=" + SuittarCommand.COMMAND_SHOW_PROFILE + "&id=" + recipient.getId() + "\">");
		buf.append(Converter.convertURL2ImgTagHtml(recipient.getProfileImageURL()));
		buf.append("</A>");			
		buf.append("</TD>");			
		
		buf.append("<TD ALIGN=\"left\"" + replayBGColor + ">");
		buf.append("<A HREF=\"http://suittar20/command?command=" + SuittarCommand.COMMAND_SHOW_PROFILE + "&id=" + leftUser.getId() + "\">");
		String prefix = "";
		if (senderIsOwner) {
			prefix = "TO&nbsp;&nbsp;:&nbsp;";
		}
		else {
			prefix = "FROM:&nbsp;";			
		}
		buf.append(prefix + leftUser.getScreenName());
		buf.append("</A>");
		buf.append("&nbsp;<A HREF=\"http://suittar20/setAtmark?id=" + leftUser.getScreenName() + "\">");
		buf.append("[@]");
		buf.append("</A>");			
		if (leftUser.getURL() != null) {
				String hp =  leftUser.getURL().toString();
				if (!hp.startsWith("http://")) {
					hp = "http://" + hp;
				}
				buf.append("&nbsp;<A HREF=\"" + hp + "\">");
				buf.append("[hp]");
				buf.append("</A>");			
		}
		buf.append("</TD>");		
		buf.append("</TR>");

		buf.append("<TR>");
		buf.append("<TD ALIGN=\"left\"" + replayBGColor + ">");
		buf.append(Converter.addAnchorTagInText(directMsg.getText()));			
		buf.append("</TD>");
		buf.append("</TR>");

		buf.append("<TR>");
		buf.append("<TD ALIGN=\"right\"" + replayBGColor + ">");
		buf.append(Converter.convertUTCString2JSTString(directMsg.getCreatedAt()));
		buf.append("</TD>");
		buf.append("</TR>");
		return buf;
	}
	
	public static StringBuffer getFooterContents(SuittarTimeLineViewBean suittarTimeLineViewBean) {
		URL nextURL = SuittarResourceManager.getResourceUrl(SuittarResourceManager.ICON_NEXT);
		URL prevURL = SuittarResourceManager.getResourceUrl(SuittarResourceManager.ICON_PREV);
		URL prevDisableURL = SuittarResourceManager.getResourceUrl(SuittarResourceManager.ICON_PREV_DISABLE);

		int page = suittarTimeLineViewBean.getPage();
		
		StringBuffer buf = new StringBuffer();
		
		buf.append("<HR WIDTH=\"100%\" SIZE=\"1\">");
		buf.append("<TABLE WIDTH=\"100%\">");
		buf.append("<TR ALIGN=\"RIGHT\">");
		buf.append("<TD>");
		if (page > 1) {
			buf.append("<A HREF=\"http://suittar20/command?command="  + SuittarCommand.COMMAND_SHOW_DIRECT_MESSAGE + "&page=" + (page - 1) + "\">");
			buf.append(Converter.convertURL2ImgTagHtml(prevURL));
			buf.append("</A>");
		}
		else {
			buf.append(Converter.convertURL2ImgTagHtml(prevDisableURL));
		}
		buf.append("&nbsp;&nbsp;");
		buf.append("<A HREF=\"http://suittar20/command?command="  + SuittarCommand.COMMAND_SHOW_DIRECT_MESSAGE + "&page=" + (page + 1) + "\">");
		buf.append(Converter.convertURL2ImgTagHtml(nextURL));
		buf.append("</A>");
		buf.append("</TD>");
		buf.append("</TR>");
		buf.append("</TABLE>");		

		return buf;
	}

	public static class DirectMessageComparator implements Comparator<DirectMessage> {
		public int compare(DirectMessage arg0, DirectMessage arg1) {
			long arg0Time = Converter.convertUTCString2JSTLong(arg0.getCreatedAt());
			long arg1Time = Converter.convertUTCString2JSTLong(arg1.getCreatedAt());

			if (arg0Time < arg1Time) {
				return 1;
			}
			else if (arg0Time > arg1Time) {
				return -1;
			}
			else {
				return 0;				
			}
		}
	}
	
}
